package app.auto.runner.base.widget;

import android.app.Dialog;
import android.content.DialogInterface;
import android.text.InputType;
import android.text.TextUtils;
import android.text.method.NumberKeyListener;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.aliang.auto.R;

import app.auto.AndroidInstance;
import app.auto.runner.base.DialogUtil;
import app.auto.runner.base.intf.FunCallback;

public class TextPicker extends AndroidInstance {
    Dialog d;


    public TextPicker setDigits(String digits) {
        this.digits = digits;
        return this;
    }

    public String txt;

    public TextPicker setTxt(String txt) {
        txt = txt == null ? "" : txt;
        this.txt = txt;
        return this;
    }

    String digits = "";
//ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-

    public void pick(final FunCallback funCallback, final FunCallback cancelCallback) {

        View v = LayoutInflater.from(getContext()).inflate(R.layout.auto_string_item_edit, null);
        ((TextView) v.findViewById(R.id.name)).setText(txt == null ? "" : txt);
        if (!TextUtils.isEmpty(digits)) {

            ((EditText) v.findViewById(R.id.name)).setKeyListener(new NumberKeyListener() {
                public int getInputType() {
                    //指定键盘类型
                    return InputType.TYPE_CLASS_TEXT;
                }

                protected char[] getAcceptedChars() {
                    //指定你所接受的字符
                    return digits.toCharArray();
                }
            });
        }

        d = DialogUtil.showDialog(v, v, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                String txt = ((TextView) d.findViewById(R.id.name)).getText().toString();
                funCallback.onCallback(txt, null);
            }
        });

        d.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                if (cancelCallback != null) {
                    cancelCallback.onCallback(null, null);
                }
                d.dismiss();
                d = null;

            }

        });
        d.show();
    }

    @Override
    public AndroidInstance use(Object mp, Object... p) {
        return null;
    }
}
